package com.lyhengtongwl.zqsnews.ui.activity;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;
import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.base.BaseActivity;
import com.lyhengtongwl.zqsnews.entity.BaseEntity;
import com.lyhengtongwl.zqsnews.net.MyCallback;
import com.lyhengtongwl.zqsnews.net.Task;
import com.lyhengtongwl.zqsnews.net.Url;
import com.lyhengtongwl.zqsnews.utils.ScreenUtil;
import com.lyhengtongwl.zqsnews.utils.TitleBuilder;
import com.lyhengtongwl.zqsnews.utils.ToastUtil;
import com.lyhengtongwl.zqsnews.widget.CountTimer;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Response;

public class FindpwdActivity extends BaseActivity {

    private boolean isGetCodeImg = true;//默认是刷新过图片验证码的
    private String phone;//手机号
    CountTimer countTimer;

    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_getcode)
    TextView tvGetcode;
    @BindView(R.id.iv_codeImg) //验证码图片
            ImageView iv_codeImg;
    @BindView(R.id.et_codeImg)//图片验证码
            EditText et_codeImg;

    @BindView(R.id.edit_phoneCode)//短信验证码
            EditText edit_phoneCode;
    @BindView(R.id.edit_pwd)//密码
            EditText edit_pwd;

    @Override

    public void setListen() {

    }

    @Override
    public void setTitleBarColor() {

    }

    @Override
    public int intiLayout() {
        return R.layout.activity_findpwd;
    }

    @Override
    public void initView() {
        new TitleBuilder(this).setTitleText("忘记密码")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });

        countTimer = new CountTimer(tvGetcode);
    }

    @Override
    public void initData() {
        phone = getIntent().getStringExtra("phone");
        etPhone.setText(phone);
        Picasso.with(App.getContext()).load(Url.BaseUrl + "/captcha/generateImage").memoryPolicy(MemoryPolicy.NO_CACHE).into(iv_codeImg);
    }

    //刷新图片验证码
    private void refreshCodeImg() {
        isGetCodeImg = true;
        et_codeImg.setText("");
        Picasso.with(App.getContext()).load(Url.BaseUrl + "/captcha/generateImage").memoryPolicy(MemoryPolicy.NO_CACHE).into(iv_codeImg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countTimer != null) {
            countTimer.cancel();
            tvGetcode.setEnabled(true);
            tvGetcode.setText("获取验证码");
        }
    }


    @OnClick({R.id.tv_getcode, R.id.tv_save, R.id.iv_codeImg})
    public void onViewClicked(View view) {
        if (ScreenUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_getcode://获取验证码
                if (!isGetCodeImg) {
                    refreshCodeImg();
                }
                if (!"".equals(et_codeImg.getText().toString())) {
                    sendVerify();
                } else {
                    ToastUtil.showShort(App.getContext(), "请输入图片验证码！");
                }
                break;
            case R.id.tv_save://保存
                if (edit_phoneCode.getText().toString().equals("")) {
                    ToastUtil.showShort(App.getContext(), "请输入图片验证码！");
                } else if (edit_pwd.getText().toString().equals("")) {
                    ToastUtil.showShort(App.getContext(), "请输入密码！");
                } else {
                    resetPwd();
                }
                break;
            case R.id.iv_codeImg://刷新图片验证码
                refreshCodeImg();
                break;
        }
    }

    private void resetPwd() {
        Task.getApiService().resetPwd(phone, edit_phoneCode.getText().toString(), edit_pwd.getText().toString()).enqueue(new MyCallback<BaseEntity>(FindpwdActivity.this) {
            @Override
            protected void onSuccess(Response<BaseEntity> response) {

                ToastUtil.showShort(App.getContext(), response.body().getMessage());
                finish();
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });
    }

    /**
     * 获取验证码
     */
    private void sendVerify() {
        countTimer.start();
        isGetCodeImg = false;
        Task.getApiService().sendPhoneCode(phone, et_codeImg.getText().toString()).enqueue(new MyCallback<BaseEntity>(FindpwdActivity.this) {
            @Override
            protected void onSuccess(Response<BaseEntity> response) {
                if ("0".equals(response.body().getCode())) {
                    ToastUtil.showShort(App.getContext(), response.body().getMessage());
                }
            }

            @Override
            protected void onFailure(Throwable t) {
                ToastUtil.showShort(App.getContext(), "发送失败！");
            }
        });
    }
}
